﻿using Microsoft.AspNetCore.Mvc;
using YDT_SeckillService.Applictions.DTOs;
using YDT_SeckillService.Applictions.Interfaces;

namespace YDT_SeckillService.Api.Controllers
{
    /// <summary>
    /// 秒杀商品控制器 - 处理秒杀商品相关的HTTP请求
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class SeckillProductsController : ControllerBase
    {
        private readonly ISeckillService _seckillService;

        /// <summary>
        /// 构造函数 - 依赖注入秒杀服务
        /// </summary>
        public SeckillProductsController(ISeckillService seckillService)
        {
            _seckillService = seckillService;
        }

        /// <summary>
        /// 减少秒杀商品库存
        /// </summary>
        /// <param name="id">秒杀商品ID</param>
        /// <param name="quantity">减少数量</param>
        /// <returns>更新后的秒杀商品信息</returns>
        [HttpPatch("{id:guid}/stock/decrease")]
        public async Task<ActionResult<SeckillProductDto>> DecreaseStock(Guid id, [FromBody] int quantity)
        {
            try
            {
                var product = await _seckillService.DecreaseSeckillProductStockAsync(id, quantity);
                if (product == null)
                {
                    return NotFound($"秒杀商品ID '{id}' 不存在");
                }
                return Ok(product);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }
}
